Syntax
Basic(BNF范式):
λ expression/termsLambda abstractionLmabda application:M,N::=x ∣ λx.M ∣ M N:λx.M:(M N)e.g.(λx.M)3
约定:
Body of λ extends as far to the right as possible(左结合):
λx.M N=λx.(M N)
Function applications are left-associative(左相关):
M N P=(M N) P,not M (N P)
Higher-order Functions
Functions can be returned as return values:
λx.λy.x−y
Functions can be passed as arguments:
(λf.λx.f x)(λx.x+1)2
Curried Functions
λ abstraction是单参数的函数(虽然计算上多参数和单参数是一样的),并且可以相互转换。
λ(x,y).x−y→Curryλx.λy.x−y
Uncurry:逆过程。
Free and Bound Variables(α-equivalence)
For λx.x+y: x is bound variable, y is free variable.
Bound variable can be renamed: λz.z+y, (x+y) is the scope of binding λx,即Bound variable是一个placeholder,占位符,改名后实际上还是保持后文提及的α-equivalence。
However, name of free variable does matter.
fv(x):fv(x):fv(λx.M):fv(M N):the set of free variables in{x}fv(M)/{x}fv(M)∪fv(N)
bound variable definiation is meaningless.
α-equivalence
λx.M=λy.M[y/x]
其中 M[y/x]表示y替换x。
Semantics
β-reduction
(λx.M)N→M[N/x]
Repeatedly apply reduction rule to any sub-term.
x[N/x]:y[N/x]:(M P)[N/x]:(λx.M)[N/x]:(λy.M)[N/x]:(λy.M)[N/x]:Ny(M[N/x])(P[N/x])λx.Mλy.(M[N/x])λz.(M[z/y][N/x])if y∈/fv(N)ify∈fv(N)&z fresh
Reduction Rules
(λx.M)N→M[N/x](β)
M N→M′ NM→M′
M N→M N′N→N′
λx.M→λx.M′M→M′
箭头表示一种规约的relation,而非等号。
subsitution 用等号,reduction 用箭头。
β-redex: a term of the form (λx.M)N(reducible expression).
β-normal form: a term containing no β-redex,即不能再β-reduction.
Church-Rosser Property(合流性)
Terms can be evaluated in any order, final result (if there is one) is uniquely determined.
Formalize:
M→∗M′M→0M′M→k+1M′M→∗M′zero−or−more steps of:iffM=M′iff∃M′′.M→M′′&M′′→kM′iff∃k.M→kM′
For all M, M1 and M2,
if M→∗M1 and M→∗M2, then there exists M’ such that
M1→∗M′ and M2→∗M′
推论:
由于α-equivalence, every term has at most one normal form.
对一个term有多个β-redex:
Good news: 无论哪个备选,至多一个normal form
Bad news: 一些规约策略可能无法找到normal form(no terminating, e.g. (λx.x x)(λx.x x)).
Reduction strategies

Normal-order reduction: choose the left-most, outer-most redex first
(Normal-order reduction will find normal form if exists)
Applicative-order reduction: choose the left-most, inner-most redex first
将reduction类比编程语言中的evaluation strategies:
- Call-by-name (类似 normal-order),实参不急着求值,而是代入到函数体里(ALGOL 60)
- Call-by-need(缓存版call-by-name),即lazy evaluation(Haskell)
- Call-by-value(类似applicative-order),即eager evaluation(C)。
subtle difference:
- under lambda,因此允许函数体内的优化,可能导致non-terminate(i.e. λx.((λy.yy)(λy.yy)))
- Evaluation strategies:不会 reduces under lambda
Evaluation
仅对closed terms(无free variable)求值,并不总是reduce至normal form,会停在包含canoical form(比如一个abstraction)上。

If normal-order reduction terminates, the reduction sequence must contain a first canonical form.
另外,evaluation按normal-order 进行。和reduction一样,evaluation也可能no terminate。
Normal-order evaluation rules:
λx.M⟹λx.M(Term)
MN⟹PM⟹λx.M′M′[N/x]⟹P(β)
small-step版:
(λx.M)N→M[N/x](β)
MN→M′NM→M′
和reduction相比少了两个规则,因为evaluation不想提前化简,只需要到canoical即可。
example:

eager evaluation:
Postpone the substitution until the argument is a canonical form.
No need to reduce many copies of the argument separately.
λx.M⟹Eλx.M(Term)
MN⟹EPM⟹Eλx.M′N⟹EN′M′[N′x]⟹EP(β)
small-step版:
(λx.M)(λy.N)→M[(λy.N)/x](β)
MN→M′NM→M′
(λx.M)N→(λx.M)N′N→N′
Programming in λ-calculus
True=False=not=and=or=if b then M else N=not′=λx.λy.xλx.λy.yλb.b False Trueλb.λb′.b b′ Falseλb.λb′.b True b′b M Nλb.λx.λy.b y x
Church numerals:
0=1=n=succ=succ′=iszero=λf.λx.xλf.λx.f xλf.λx.fn xλn.λf.λx.f(n f x)λn.λf.λx.n f(f x)λn.λx.λy.n(λz.y)x
Pairs/Tuple:
(M,N)=π0=π0=(M1,...,Mn)=πi=λf.fMNλp.p(λx.λy.x)λp.p(λx.λy.y)λf.fM1 ... Mnλp.p(λx1...λxn. xi)
Fix point
如何对类fact(n)=if(n==0) then 1 else n∗fact(n−1)的递归函数进行进行编码?
在数学上,我们对不动点的定义为f(x)=x,对函数而言,可以有0、有限到无限个不动点。
上面的函数我们可以转换为:fact=(λf.λn.if(n==0)then 1 else n∗f(n−1))fact。而对此,设:
F=λf.λn.if(n==0)then 1 else n∗f(n−1)
那么有:fact=βF fact,fact是F的不动点。
Fixpoint combinator是更高层的function~h,其满足:
对所有的函数f,(h f)的都是f的不动点:h f=βf (h f)
对此,有:
Turing's fixpoint combinator Θ: A=λx.λy.y(x x y),Θ=A A
Curry's fixpoint combinator Y: λf.(λx.f(x x))(λx.f(x x))